package com.xps.purifierapp.view.fragment;

import android.content.Intent;
import android.graphics.Picture;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.kcode.autoscrollviewpager.view.AutoScrollViewPager;
import com.kcode.autoscrollviewpager.view.BaseViewPagerAdapter;
import com.squareup.picasso.Picasso;
import com.xps.purifierapp.R;
import com.xps.purifierapp.bean.NotInstalledIBean;
import com.xps.purifierapp.date.net.UserNetWorks;
import com.xps.purifierapp.utils.AppCommond;
import com.xps.purifierapp.view.activity.InstallationdetailsActivity;
import com.xps.purifierapp.view.activity.MainActivity;
import com.zhy.autolayout.AutoLinearLayout;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import rx.Subscriber;

/**
 * Created by liwenhan on 2017/11/6.
 */

public class HomeFragment extends Fragment implements View.OnClickListener {
    ImageView actionbarIvBack;
    TextView actionbarTvTitle;
    AutoLinearLayout Linear_Need_to_complete;
    AutoLinearLayout Linear_Completed;
    AutoLinearLayout Linear_Accept;
    AutoLinearLayout LinearLayout_Need_to_complete;
    AutoLinearLayout LinearLayout_Completed;
    AutoLinearLayout LinearLayout_Accept;
    ImageView Image_Need_to_complete;
    ImageView Image_Completed;
    ImageView Image_Accept;
    TextView TextView_Translate;
    TextView TextView_Personnel;
    TextView TextView_Place;
    AutoScrollViewPager autoScrollViewPager;
    ImageView ivMsg;
    private BaseViewPagerAdapter<String> mBaseViewPagerAdapter;
    private String[] paths = {"http://jsy.xpskj.com/static/images/BZ.jpg",
            "http://jsy.xpskj.com/static/images/DZ.jpg"};

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, null);
        ButterKnife.bind(this, view);
        actionbarIvBack = view.findViewById(R.id.actionbar_iv_back);
        actionbarTvTitle = view.findViewById(R.id.actionbar_tv_title);
        autoScrollViewPager = view.findViewById(R.id.viewPager);
        actionbarIvBack.setVisibility(View.GONE);
        actionbarTvTitle.setText("我的主页");
        Linear_Need_to_complete = view.findViewById(R.id.Linear_Need_to_complete);
        Linear_Completed = view.findViewById(R.id.Linear_Completed);
        Linear_Accept = view.findViewById(R.id.Linear_Accept);
        LinearLayout_Need_to_complete = view.findViewById(R.id.LinearLayout_Need_to_complete);
        LinearLayout_Completed = view.findViewById(R.id.LinearLayout_Completed);
        LinearLayout_Accept = view.findViewById(R.id.LinearLayout_Accept);
        Image_Need_to_complete = view.findViewById(R.id.Image_Need_to_complete);
        Image_Completed = view.findViewById(R.id.Image_Completed);
        Image_Accept = view.findViewById(R.id.Image_Accept);
        ivMsg = view.findViewById(R.id.iv_msg);
        LinearLayout_Need_to_complete.setOnClickListener(this);
        LinearLayout_Completed.setOnClickListener(this);
        LinearLayout_Accept.setOnClickListener(this);
        ivMsg.setOnClickListener(this);
        mBaseViewPagerAdapter = new BaseViewPagerAdapter<String>(getActivity(), listener) {
            @Override
            public void loadImage(ImageView view, int position, String url) {
                Picasso.with(getActivity()).load(url).into(view);
            }

            @Override
            public void setSubTitle(TextView textView, int position, String s) {
            }
        };

//        Glide.with(getActivity()).load("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1510232656492&di=1a21a75e7f579a6e6524a3473a9cf2a2&imgtype=0&src=http%3A%2F%2Fpic1.16pic.com%2F00%2F15%2F58%2F16pic_1558677_b.jpgm")
//                .into(autoScrollViewPager);
      autoScrollViewPager.setAdapter(mBaseViewPagerAdapter);
        NotInstalled();
        Completed();
      mBaseViewPagerAdapter.add(initData());

        return view;
    }

    private List<String> initData() {
        List<String> data = new ArrayList<>();
        Picture picture;
        for (int i = 0; i < paths.length; i++) {
            data.add(paths[i]);
        }
        return data;
    }


    public void hide() {
        FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.hide(this);
        fragmentTransaction.commit();
    }

    private BaseViewPagerAdapter.OnAutoViewPagerItemClickListener listener = new BaseViewPagerAdapter.OnAutoViewPagerItemClickListener<String>() {

        @Override
        public void onItemClick(int position, String url) {
        }
    };

    public void NotInstalled() {
        UserNetWorks.getNotInstalled("10", new Subscriber<NotInstalledIBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
            }

            @Override
            public void onNext(NotInstalledIBean notInstalledIBean) {
                if ("SUCCESS".equals(notInstalledIBean.getReturn_code())) {
                    Linear_Need_to_complete.removeAllViews();
                    List<NotInstalledIBean.ReturnBodyBean> list = notInstalledIBean.getReturn_body();
                    if (list != null && list.size() > 0) {
                        for (int i = 0; i < list.size(); i++) {
                            addOrderItem(list.get(i), Linear_Need_to_complete);
                            addDividerLine(Linear_Need_to_complete);
                        }
                    }
                }
            }
        });
    }

    public void Completed() {
        UserNetWorks.getCompleted("10", new Subscriber<NotInstalledIBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
            }

            @Override
            public void onNext(NotInstalledIBean notInstalledIBean) {
                if ("SUCCESS".equals(notInstalledIBean.getReturn_code())) {
                    Linear_Completed.removeAllViews();
                    List<NotInstalledIBean.ReturnBodyBean> list = notInstalledIBean.getReturn_body();
                    if (list != null && list.size() > 0) {
                        for (int i = 0; i < list.size(); i++) {
                            addOrderItem(list.get(i), Linear_Completed);
                            addDividerLine(Linear_Completed);
                        }
                    }
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.LinearLayout_Accept:
                if (Linear_Accept.getVisibility() == View.GONE) {
                    Linear_Accept.setVisibility(View.VISIBLE);
                    Image_Accept.setImageResource(R.mipmap.image_top);
                } else {
                    Linear_Accept.setVisibility(View.GONE);
                    Image_Accept.setImageResource(R.mipmap.image_button);
                }
                break;
            case R.id.LinearLayout_Completed:
                if (Linear_Completed.getVisibility() == View.GONE) {
                    Linear_Completed.setVisibility(View.VISIBLE);
                    Image_Completed.setImageResource(R.mipmap.image_top);
                } else {
                    Linear_Completed.setVisibility(View.GONE);
                    Image_Completed.setImageResource(R.mipmap.image_button);
                }
                break;
            case R.id.LinearLayout_Need_to_complete:
                if (Linear_Need_to_complete.getVisibility() == View.GONE) {
                    Linear_Need_to_complete.setVisibility(View.VISIBLE);
                    Image_Need_to_complete.setImageResource(R.mipmap.image_top);
                } else {
                    Linear_Need_to_complete.setVisibility(View.GONE);
                    Image_Need_to_complete.setImageResource(R.mipmap.image_button);
                }
                break;
            case R.id.iv_msg:
                AppCommond appCommond = new AppCommond.Builder()
                        .setOpration(AppCommond.CLOSE_ACTIVITY)
                        .setRequestcloseAcname(MainActivity.class.getSimpleName())
                        .build();
                EventBus.getDefault().post(appCommond);
                break;
        }
    }


    /**
     * 添加分割线
     */
    public void addDividerLine(LinearLayout ll_lv) {
        View v = new View(getActivity());
        v.setAlpha(0.5f);
        v.setBackgroundColor(getActivity().getResources().getColor(R.color.dialogplus_card_shadow));
        v.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 1));
        ll_lv.addView(v);
    }

    /**
     * 添加订单
     * addOrderItem(DingDanBean.ReturnBodyBean dataBean)
     */
    public void addOrderItem(final NotInstalledIBean.ReturnBodyBean dataBean, LinearLayout ll_lv) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.order_daijia_item, null);
        TextView_Translate = view.findViewById(R.id.TextView_Translate);
        TextView_Personnel = view.findViewById(R.id.TextView_Personnel);
        TextView_Place = view.findViewById(R.id.TextView_Place);
        TextView_Translate.setText(dataBean.getProduct_type());
        TextView_Personnel.setText("联系人:" + dataBean.getClient_name());
        TextView_Place.setText("地址:" + dataBean.getProvince() + dataBean.getCity() + dataBean.getDetail_address());
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toMainAc(dataBean);
            }
        });
        ll_lv.addView(view);
    }

    public void toMainAc(NotInstalledIBean.ReturnBodyBean dataBean) {
        Intent intent = new Intent(getActivity(), InstallationdetailsActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("dataBean", dataBean);
        getActivity().startActivity(intent);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
